/**
 * 
 */
package dms.presentation.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.springframework.context.ApplicationContext;

import dms.model.role.Account;
import dms.model.role.DormSupervisor;
import dms.model.role.Role;
import dms.model.role.Student;
import dms.model.role.StudentAffairTeacher;
import dms.model.role.SystemAdministrator;
import dms.model.role.wrapper.DormSupervisorWrapper;
import dms.model.role.wrapper.StudentAffairTeacherWrapper;
import dms.model.role.wrapper.StudentWrapper;
import dms.model.role.wrapper.SystemAdministratorWrapper;
import dms.presentation.form.AccountForm;
import dms.presentation.form.LoginForm;
import dms.service.AccountService;
import dms.service.AuthenticationService;
import dms.service.StudentQueryService;
import dms.service.SystemAdministratorQuerySerivce;
import dms.util.Constant;
import dms.util.ContextUtil;

/**
 * @author 李健
 *
 */
public class LoginAction extends Action {
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HttpSession session = request.getSession(true);
		ApplicationContext appContext =	ContextUtil.getContext();
		AuthenticationService authenticationService = (AuthenticationService) appContext.getBean("authenticationService");
		
		LoginForm accountForm = (LoginForm)form;
		
		String name = accountForm.getName();
		String password = accountForm.getPassword();
		
		boolean check = false;
		try {
			check = authenticationService.checkAuthentication(name, password);
		} catch (Exception e) {
			
		}
		
		if (!check) {
			ActionMessages errors = new ActionMessages();
			errors.add("checkError", new ActionMessage("用户名或密码错误", false));
			this.addErrors(request, errors);
			return mapping.getInputForward();
		}
		
		AccountService accountService = (AccountService) appContext.getBean("accountService");
		Account account = accountService.getAccount(name);
		
		session.setAttribute(Constant.ACCOUNT, account);
		
		SystemAdministratorQuerySerivce systemAdministratorQuerySerivce = (SystemAdministratorQuerySerivce) appContext.getBean("systemAdministratorQuerySerivce");
		StudentQueryService studentQueryService = (StudentQueryService) appContext.getBean("studentQueryService");
		
		SystemAdministrator systemAdministrator = null;
		Student student = null;
		StudentAffairTeacher studentAffairTeacher = null;
		DormSupervisor dormSupervisor = null;
		
		
		if (account.getRole().equals(Role.DORM_SUPERVISOR)) {
			dormSupervisor = systemAdministratorQuerySerivce.findDormSupervisorById(account.getId());
			DormSupervisorWrapper dormSupervisorWrapper = (DormSupervisorWrapper) appContext.getBean("dormSupervisorWrapper");
			dormSupervisorWrapper.setDormSupervisor(dormSupervisor);
			session.setAttribute(Constant.USER, dormSupervisorWrapper);
			
			return mapping.findForward(Role.DORM_SUPERVISOR.getRoleName());
		} else if (account.getRole().equals(Role.STUDENT)) {
			student = studentQueryService.findById(account.getId());
			StudentWrapper studentWrapper = (StudentWrapper) appContext.getBean("studentWrapper");
			studentWrapper.setStudent(student);
			session.setAttribute(Constant.USER, studentWrapper);
			
			return mapping.findForward(Role.STUDENT.getRoleName());
		} else if (account.getRole().equals(Role.STUDENT_AFFAIR_TEACHER)) {
			studentAffairTeacher = systemAdministratorQuerySerivce.findStudentAffairTeacherById(account.getId());
			StudentAffairTeacherWrapper studentAffairTeacherWrapper = (StudentAffairTeacherWrapper) appContext.getBean("studentAffairTeacherWrapper");
			studentAffairTeacherWrapper.setStudentAffairTeacher(studentAffairTeacher);
			session.setAttribute(Constant.USER, studentAffairTeacherWrapper);
			
			return mapping.findForward(Role.STUDENT_AFFAIR_TEACHER.getRoleName());
		} else {
			SystemAdministratorWrapper systemAdministratorWrapper = (SystemAdministratorWrapper) appContext.getBean("systemAdministratorWrapper");
			systemAdministrator = systemAdministratorQuerySerivce.findSystemAdministratorById(account.getId());
			systemAdministratorWrapper.setSystemAdministrator(systemAdministrator);
			session.setAttribute(Constant.USER, systemAdministratorWrapper);
		
			return mapping.findForward(Role.SYSTEM_ADMINISTRATOR.getRoleName());
		}
		
		
	}
}
